package com.heroCW.bodhiAttic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heroCW.bodhiAttic.dto.DishDto;
import com.heroCW.bodhiAttic.entity.Dish;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.CategoryService;
import com.heroCW.bodhiAttic.service.DishFlavorService;
import com.heroCW.bodhiAttic.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody DishDto dishDto) {
        log.info("dishDto: {}", dishDto.toString());

        Boolean flag = dishService.saveWithFlavor(dishDto);
        if (flag) {
            return Result.success(ResultCode.SAVE_OK, "新增菜品成功");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "新增菜品失败");
        }
    }

    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<DishDto>> page(int page, int pageSize, String name) {

        IPage<DishDto> dishDtoPage = dishService.getPage(page, pageSize, name);

        // 删除一个并重新查询时，如果不够分页，则以页数最大值再查询一次
        if (page > dishDtoPage.getPages()) {
            dishDtoPage = dishService.getPage((int) dishDtoPage.getPages(), pageSize, name);
        }
        if (dishDtoPage != null) {
            return Result.success(ResultCode.GET_OK, dishDtoPage);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到菜品数据");
        }
    }

    /**
     * 根据id查询菜品信息和对应的口味
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishDto> dishEcho(@PathVariable Long id) {

        DishDto dishDto = dishService.getByIdWithFlavor(id);
        if (dishDto != null) {
            return Result.success(ResultCode.GET_OK, dishDto);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到菜品数据");
        }
    }

    /**
     * 根据条件查询对应的菜品数据
     *
     * @param dish
     * @return
     */
    @GetMapping({"/front/list", "/backend/list"})
    public Result<List<DishDto>> list(Dish dish) {
        log.info("dish: {}", dish.toString());
        List<DishDto> dishDtoList = dishService.getList(dish);

        if (dishDtoList != null) {
            return Result.success(ResultCode.GET_OK, dishDtoList);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到菜品数据");
        }
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto) {
        log.info("dishDto: {}", dishDto.toString());

        Boolean flag = dishService.updateWithFlavor(dishDto);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "修改菜品成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "修改菜品失败");
        }
    }

    /**
     * 删除或批量删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam("ids") List<Long> ids) {
        log.info("ids: {}", ids);

        Boolean flag = dishService.removeDishWithFlavor(ids);
        if (flag) {
            return Result.success(ResultCode.DELETE_OK, "菜品删除成功");
        } else {
            return Result.error(ResultCode.DELETE_ERR, "菜品删除失败");
        }
    }

    /**
     * 修改或批量修改菜品状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@PathVariable Integer status, @RequestParam("ids") List<Long> ids) {

        Boolean flag = dishService.updateStatus(status, ids);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "菜品状态修改成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "菜品状态修改失败");
        }
    }
}
